package com.cloudkinto.service.dic.impl;

import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.entity.DicBusinessDo;
import com.cloudkinto.dao.DicBusinessDao;
import com.cloudkinto.service.dic.DicBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.service.dic.vo.dicBusiness.DicBusinessAddReq;
import com.cloudkinto.service.dic.vo.dicBusiness.DicBusinessDetailRes;
import com.cloudkinto.service.dic.vo.dicBusiness.DicBusinessListPageRes;
import com.cloudkinto.service.dic.vo.dicBusiness.DicBusinessUpdateReq;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;

/**
 * <p>
 * 业务码表主表 service实现类
 * </p>
 * @author 张永远
 * @since 2020-05-19
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class DicBusinessServiceImpl extends ServiceImpl<DicBusinessDao, DicBusinessDo> implements DicBusinessService {
    @Autowired
    private DicBusinessDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public DicBusinessAddReq add(DicBusinessAddReq req, Long userId) throws BizException {
        addCheck(req);
        DicBusinessDo entityDo = new DicBusinessDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(DicBusinessAddReq req) throws BizException {
        QueryWrapper<DicBusinessDo> wrapper = new QueryWrapper<>();
        wrapper.eq("dic_name",req.getDicName());
        int count = dao.selectCount(wrapper);
        if(count > 0){
            throw new BizException(SysConstant.DicBusiness_Check_Name);
        }
    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(DicBusinessDo entityDo, Long userId) {
    }

    //将新增的do转换成要返回的实体
    private DicBusinessAddReq handlerAddAfter(DicBusinessDo entityDo) {
        DicBusinessAddReq res = new DicBusinessAddReq();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success(detail(id));
    }

    @Override
    public DicBusinessUpdateReq update(DicBusinessUpdateReq req, Long userId) throws BizException {
        updateCheck(req);
        DicBusinessDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);
    }

    //编辑校验
    private void updateCheck(DicBusinessUpdateReq req) throws BizException {
        QueryWrapper<DicBusinessDo> wrapper = new QueryWrapper<>();
        wrapper.eq("dic_name",req.getDicName()).ne("id",req.getId());
        int count = dao.selectCount(wrapper);
        if(count > 0){
            throw new BizException(SysConstant.DicBusiness_Check_Name);
        }
    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(DicBusinessDo entityDo, Long userId) {
    }

    //做修改之后要执行的业务定制处理
    private DicBusinessUpdateReq handlerUpdateAfter(DicBusinessDo entityDo) {
        DicBusinessUpdateReq res = new DicBusinessUpdateReq();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        DicBusinessDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(DicBusinessDo entityDo, Long userId) {
    }

    @Override
    public DicBusinessDetailRes detail(Long id) {
        DicBusinessDo entityDo =  dao.selectById(id);
        DicBusinessDetailRes res = new DicBusinessDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private DicBusinessDetailRes handlerDetailAfter(DicBusinessDetailRes res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<DicBusinessListPageRes> getList(Map<String, String> map) {
        QueryWrapper<DicBusinessDo> wrapper = queryBuild(map);
        List<DicBusinessDo> list = this.list(wrapper);
        List<DicBusinessListPageRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<DicBusinessDo> wrapper = queryBuild(map);
        IPage<DicBusinessDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<DicBusinessListPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int)page.getCurrent(), (int)page.getSize(), (int)page.getTotal(), (int)page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<DicBusinessListPageRes> handlerListAfter(List<DicBusinessDo> list ){
        List<DicBusinessListPageRes> result = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            for (DicBusinessDo businessDo : list) {
                DicBusinessListPageRes res = new DicBusinessListPageRes();
                BeanUtils.copyProperties(businessDo,res);
                result.add(res);
            }
        }
        return result;
    }

    private IPage<DicBusinessDo> pageInit(Map<String, String> map) {
        IPage<DicBusinessDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<DicBusinessDo> queryBuild(Map<String, String> map) {
        QueryWrapper<DicBusinessDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


}
